﻿using System.Web.Mvc;
using System.Web.Routing;
using Sendz.Domain;
using Sendz.WebUI.Models;
using Sendz.WebUI.Infrastructure;
using Sendz.WebUI.Binders;
using Sendz.Validators;
using System.Collections.Generic;

namespace Sendz.WebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // Custom route for short form paths (vst.me etc.)
            routes.MapRoute(
                "FormShortPath", //Route Name
                "{shortpath}", //URL with parameters
                new { controller = "Form", action = "Display" } //Paremeter defaults
            );

            // Normal routes
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Form", action = "List", id = UrlParameter.Optional } // Parameter defaults
            );

            // Reference for "list" actions since this somehow gets dropped in default actionresult call
            routes.MapRoute(
                "FormList", "Form/List", new { controller = "Form", action = "List" }
            );

            routes.MapRoute(
                "AutoResponderList", "AutoResponder/List", new { controller = "AutoResponder", action = "List" }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
            // Model binders
            ModelBinders.Binders[typeof(IEnumerable<FormFieldViewModel>)] = new FormFieldModelBinder();
            // IoC factory
            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
            // Custom validations (domain level)
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredIfAttribute), typeof(RequiredIfValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredWhenContainsAttribute), typeof(RequiredWhenContainsValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RegExRemoteAttribute), typeof(RegExRemoteValidator));
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MustMatchAttribute), typeof(MustMatchValidator));
        }
    }
}